package com.ccdt.tv.roundprogram.round;

import com.blankj.utilcode.util.LogUtils;
import com.ccdt.tv.roundprogram.bean.ChannelRoot;
import com.ccdt.tv.roundprogram.bean.PlayInfo;
import com.ccdt.tv.roundprogram.http.Api;
import com.ccdt.tv.roundprogram.util.XmlHelper;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * create by hfq 2018/10/16
 */
public class RoundModel implements RoundContract.Model {

    private final String TAG = "RoundModel";

    private RequestBody mRequestBody;

    @Override
    public Observable<ChannelRoot> getChannel(String client) {
        XmlHelper util = XmlHelper.getInstance();
        MediaType type = MediaType.parse("application/xml");
        mRequestBody = RequestBody.create(type, util.writeGetChannelXmlSerial(client));
        return Api.getInstance().getChannels(mRequestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<PlayInfo> getPlayInfo(int position, ChannelRoot channelRoot) {
        XmlHelper util = XmlHelper.getInstance();
        MediaType type = MediaType.parse("application/xml");
        String xmlString = util.writePlayInfoXmlSerial(channelRoot.getChannel().get(position).getChannelCode());
        LogUtils.eTag(TAG, "play url xml request : " + xmlString);
        mRequestBody = RequestBody.create(type, xmlString);
        return Api.getInstance().getPlayInfo(mRequestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

}
